<?php

namespace app\controller\api\controller;

use app\support\WebSocketHelper;
use app\support\ResponseHelper;
use support\Request;
use support\Response;
use Shayvmo\WebmanAnnotations\Annotations\PostMapping;
use Shayvmo\WebmanAnnotations\Annotations\GetMapping;

/**
 * WebSocket示例控制器
 * 演示如何在业务代码中使用WebSocket功能
 */
class WebSocketController
{
    /**
     * 广播消息给所有客户端
     */
    #[PostMapping(path: '/websocket/broadcast')]
    public function broadcast(Request $request): Response
    {
        $message = $request->input('message', '');
        
        if (empty($message)) {
            return ResponseHelper::failJson('消息内容不能为空');
        }
        
        // 使用WebSocket助手类广播消息
        WebSocketHelper::broadcast($message, 'admin_broadcast');
        
        return ResponseHelper::successJson('', '消息广播成功');
    }
    
    /**
     * 向指定客户端发送消息
     */
    #[PostMapping(path: '/websocket/send')]
    public function sendToClient(Request $request): Response
    {
        $clientId = $request->input('clientId', '');
        $message = $request->input('message', '');
        
        if (empty($clientId) || empty($message)) {
            return ResponseHelper::failJson('客户端ID和消息内容不能为空');
        }
        
        $success = WebSocketHelper::sendToClient($clientId, $message);
        
        if ($success) {
            return ResponseHelper::successJson('', '消息发送成功');
        } else {
            return ResponseHelper::failJson('消息发送失败，客户端可能不在线');
        }
    }
    
    /**
     * 发送订单状态更新通知
     */
    #[PostMapping(path: '/websocket/order-status')]
    public function notifyOrderStatus(Request $request): Response
    {
        $orderId = $request->input('orderId', '');
        $status = $request->input('status', 0);
        $clientId = $request->input('clientId', ''); // 可选，为空则广播给所有人
        
        if (empty($orderId) || !in_array($status, [1, 2, 3, 4, 5, 6])) {
            return ResponseHelper::failJson('订单ID或状态参数无效');
        }
        
        WebSocketHelper::notifyOrderStatus($orderId, (int)$status, $clientId);
        
        return ResponseHelper::successJson('', '订单状态通知发送成功');
    }
    
    /**
     * 发送新订单通知
     */
    #[PostMapping(path: '/websocket/new-order')]
    public function notifyNewOrder(Request $request): Response
    {
        $orderData = $request->input('orderData', []);
        
        if (empty($orderData)) {
            return ResponseHelper::failJson('订单数据不能为空');
        }
        
        WebSocketHelper::notifyNewOrder($orderData);
        
        return ResponseHelper::successJson('', '新订单通知发送成功');
    }
    
    /**
     * 发送系统通知
     */
    #[PostMapping(path: '/websocket/system-notify')]
    public function notifySystem(Request $request): Response
    {
        $title = $request->input('title', '');
        $content = $request->input('content', '');
        $level = $request->input('level', 'info'); // info, warning, error
        
        if (empty($title) || empty($content)) {
            return ResponseHelper::failJson('标题和内容不能为空');
        }
        
        if (!in_array($level, ['info', 'warning', 'error'])) {
            $level = 'info';
        }
        
        WebSocketHelper::notifySystem($title, $content, $level);
        
        return ResponseHelper::successJson('', '系统通知发送成功');
    }
    
    /**
     * 获取WebSocket连接状态信息
     */
    #[GetMapping(path: '/websocket/status')]
    public function getStatus(): Response
    {
        $onlineCount = WebSocketHelper::getOnlineCount();
        $onlineClientIds = WebSocketHelper::getOnlineClientIds();
        
        $data = [
            'onlineCount' => $onlineCount,
            'onlineClientIds' => $onlineClientIds,
            'timestamp' => date('Y-m-d H:i:s')
        ];
        
        return ResponseHelper::successJson($data, '获取状态成功');
    }
    
    /**
     * 检查指定客户端是否在线
     */
    #[GetMapping(path: '/websocket/check-online')]
    public function checkClientOnline(Request $request): Response
    {
        $clientId = $request->input('clientId', '');
        
        if (empty($clientId)) {
            return ResponseHelper::failJson('客户端ID不能为空');
        }
        
        $isOnline = WebSocketHelper::isClientOnline($clientId);
        
        $data = [
            'clientId' => $clientId,
            'isOnline' => $isOnline,
            'timestamp' => date('Y-m-d H:i:s')
        ];
        
        return ResponseHelper::successJson($data, '检查完成');
    }
}